namespace comp {
    export class JoyTable extends eui.Group implements GameController {
        private btn: egret.DisplayObject   //按钮
        private bg: egret.DisplayObject //背景
        private centerX: number = 0
        private centerY: number = 0
        private bgRadius: number = 0
        private btnRadius: number = 0
        private touchID: number          //触摸ID
        private myParent: eui.Group

        public Angle: number    //0~2Pi
        public Distance: number //0~1

        public constructor() {
            super()
            this.name = "JoyTable"
        }
        public Type(): GameControllerKind { return GameControllerKind.JoyTable }

        public noSkin() {
            this.bgRadius = 120
            this.btnRadius = 30

            this.bg = ui.drawRect({
                lineColor: Color.GRAY,
                lineAlpha: 0.8,
                fillColor: Color.GRAY,
                fillAlpha: 0.2,
                width: 2 * this.bgRadius,
                height: 2 * this.btnRadius,
                centered: true,
            })
            this.addChild(this.bg)

            this.btn = ui.drawRect({
                lineColor: Color.GRAY,
                lineAlpha: 0.8,
                fillColor: Color.GRAY,
                fillAlpha: 0.6,
                width: this.btnRadius * 2,
                height: this.btnRadius * 2,
                centered: true,
            })

            let joy = this
            this.btn["onDragMove"] = function (m: hjc.MoveParam) {
                joy.Distance = (joy.btn.x - joy.centerX) / (joy.bgRadius - joy.btnRadius)
                if (joy.Distance > 0) {
                    joy.Angle = 1
                    if (joy.Distance > 1) {
                        joy.Distance = 1
                    }
                } else if (joy.Distance == 0) {
                    joy.Angle = 0
                } else {
                    joy.Angle = -1
                    if (joy.Distance < -1) {
                        joy.Distance = -1
                    }
                    joy.Distance = - joy.Distance
                }
                game.notify(ntf.JoyStickChange, { Target: this, Distance: this.Distance, Angle: this.Angle }, this)
            }
            this.addChild(this.btn)
            this.initData()
        }

        private initData() {
            this.bgRadius = this.bg.width / 2
            this.btnRadius = this.btn.height / 2
            this.centerX = this.bg.width / 2
            this.centerY = this.bg.height / 2
            this.anchorOffsetX = this.centerX
            this.anchorOffsetY = this.centerY
            this.bg.x = this.centerX
            this.bg.y = this.centerY
            this.btn.x = this.centerX
            this.btn.y = this.centerY
        }

        public start(parent: eui.Group) {
            this.myParent = parent
            this.myParent.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onTouchBegin, this);
            this.visible = false
            this.myParent.addChild(this)
            ui.setDragger(this.btn, { scope: new egret.Rectangle(this.bgRadius + this.btnRadius, this.btnRadius * 2, this.bgRadius * 2 - this.btnRadius * 2, 1) })
            this.myParent.addEventListener(egret.TouchEvent.TOUCH_END, this.onTouchEnd, this);
        }
        public stop() {
            this.myParent.removeEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onTouchBegin, this);
            this.myParent.removeEventListener(egret.TouchEvent.TOUCH_END, this.onTouchEnd, this);
            this.myParent = null
        }

        private onTouchBegin(e: egret.TouchEvent) {
            console.debug("onTouchBegin")
            let x = e.localX
            let y = e.localY
            this.touchID = e.touchPointID;
            this.x = x;
            this.y = y;
            this.btn.x = this.centerX;
            this.btn.y = this.centerY;
            this.visible = true
            hjc.DraggerManager.instance<hjc.DraggerManager>()['_onBegin']({ currentTarget: this.btn, stageX: e.stageX, stageY: e.stageY })
            game.notify(ntf.JoyStickBegin, { Target: this })
        }
        private onTouchEnd(e: egret.TouchEvent) {
            if (this.touchID != e.touchPointID) {
                return;
            }
            this.visible = false
            game.notify(ntf.JoyStickEnd, { Target: this })
        }
    }
}